<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="position: relative; top: 10px">
          <i class="el-icon-search"></i>
          搜索
        </span>
        <el-button-group style="float: right; padding: 3px 0">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="btnSearch"
            >搜索</el-button
          >
          <el-button
            size="small"
            icon="el-icon-refresh"
            type="primary"
            @click="btnReset"
            >重置</el-button
          >
        </el-button-group>
      </div>
      <el-form :model="searchForm" :inline="true" ref="productFormRef">
        <el-row>
          <el-col :span="colSize">
            <el-form-item label="商品名称" prop="name">
              <el-input
                v-model="searchForm.name"
                placeholder="商品名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="colSize">
            <el-form-item label="商品描述" prop="detail">
              <el-input
                v-model="searchForm.detail"
                placeholder="商品描述"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="colSize">
            <el-form-item label="商品分类" prop="typeId">
              <el-cascader
                @change="handleCateChange"
                v-model="productCateValue"
                :options="productCateOptions"
                :props="{ expandTrigger: 'hover', label: 'name', value: 'id' }"
              >
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="colSize">
            <el-form-item label="上架状态" prop="isOnSale">
              <el-select v-model="searchForm.isOnSale" placeholder="请选择">
                <el-option
                  v-for="item in publishStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="colSize">
            <el-form-item label="商品品牌" prop="brandId">
              <el-select
                v-model="searchForm.brandId"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in productBrandList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="colSize">
            <el-form-item label="是否上新" prop="isNew">
              <el-select v-model="searchForm.isNew" placeholder="请选择">
                <el-option
                  v-for="item in publishNewOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="colSize + 1">-->
<!--            <el-form-item label="创建时间" prop="date">-->
<!--              <el-date-picker-->
<!--                v-model="searchForm.date"-->
<!--                type="daterange"-->
<!--                range-separator="至"-->
<!--                start-placeholder="开始日期"-->
<!--                end-placeholder="结束日期"-->
<!--              >-->
<!--              </el-date-picker>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>
      </el-form>
    </el-card>
    <el-card class="data-card">
      <div slot="header" class="clearfix">
        <span style="position: relative; top: 7px">
          <i class="el-icon-tickets"></i>
          商品列表
        </span>
        <div style="float: right; padding: 3px 0">
          <el-button
            size="small"
            type="warning"
            icon="el-icon-plus"
            @click="btnAdd"
            >添加</el-button
          >
          <!--          <el-button-->
          <!--            size="small"-->
          <!--            type="danger"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="btnDelete"-->
          <!--            >批量删除</el-button-->
          <!--          >-->
        </div>
      </div>
      <el-table fit :data="productList" stripe style="width: 100%">
        <el-table-column align="center" type="selection" width="55">
        </el-table-column>
        <el-table-column align="center" type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column align="center" prop="id" label="编号" width="50">
        </el-table-column>
        <el-table-column align="center" label="图片" width="120">
          <template slot-scope="scope">
            <el-image style="height: 100px" :src="baseUrl + scope.row.file">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" label="分类" prop="typeName">
        </el-table-column>
        <el-table-column align="center" width="120" label="商品名称">
          <template slot-scope="scope">
            <p>{{ scope.row.name }}</p>
            <p>品牌：{{ scope.row.brandName }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" label="价格">
          <template slot-scope="scope">
            <p>￥{{ scope.row.price }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" label="标签">
          <template slot-scope="scope">
            <p>
              上架：
              <el-switch
                style="display: block"
                v-model="scope.row.isOnSale"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handleStatus('onSale', scope.row)"
              >
              </el-switch>
            </p>
            <p>
              新品：
              <el-switch
                style="display: block"
                v-model="scope.row.isNew"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="handleStatus('new', scope.row)"
              >
              </el-switch>
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" label="库存" prop="stock" width="80">
        </el-table-column>
        <el-table-column
          align="center"
          prop="monthlySales"
          label="月销量"
          width="100"
        >
        </el-table-column>
        <el-table-column align="center" label="总销量" prop="totalSales">
        </el-table-column>
        <el-table-column width="100" align="center" label="操作">
          <template slot-scope="scope">
            <p>
              <el-button
                size="mini"
                icon="el-icon-view"
                @click="btnView(scope.row)"
                >查看</el-button
              >
            </p>
            <p>
              <el-button
                size="mini"
                icon="el-icon-edit"
                @click="btnEdit(scope.row)"
                >编辑</el-button
              >
            </p>
            <p>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="btnDelete(scope.row)"
                >删除</el-button
              >
            </p>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { api_productList } from '../../api/product'
import { api_updateProductStatus } from '../../api/product'
import { api_listWithChildren } from '../../api/productCate'
import { api_allBrandList } from '../../api/brand'
import { api_deleteProduct } from '../../api/product'

export default {
  data() {
    return {
      colSize: 6,
      baseUrl: 'http://localhost:88/upload/',
      productCateValue: [],
      productList: [],
      productBrandList: [],
      publishStatusOptions: [
        {
          value: 1,
          label: '上架',
        },
        {
          value: 0,
          label: '下架',
        },
      ],
      publishNewOptions: [
        {
          value: 1,
          label: '是',
        },
        {
          value: 0,
          label: '否',
        },
      ],
      verifyStatusOptions: [
        {
          value: 1,
          label: '审核通过',
        },
        {
          value: 0,
          label: '未审核',
        },
      ],
      productCateOptions: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        brandId: null,
        name: null,
        detail: null,
        typeId: null,
        status: null,
        date: null,
        startDate: null,
        endDate: null,
        isOnSale: null,
        isNew: null,
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        totalPage: 3,
        total: 11,
      },
    }
  },
  created() {
    this.btnSearch()
    this.handleProductCate()
    this.handleProductBrandList()
  },
  filters: {
    verifyStatusFilter(value) {
      //上架 售空 下架
      let text = '已上架'
      if (value === 1) {
        text = '未通过'
      }
      return text
    },
  },
  methods: {
    btnSearch() {
      this.searchForm.typeId = this.productCateValue[1]
        ? this.productCateValue[1]
        : null
      this.searchForm.startDate = this.searchForm.date
        ? this.searchForm.date[0]
        : null
      this.searchForm.endDate = this.searchForm.date
        ? this.searchForm.date[1]
        : null
      this.handleProductList()
    },
    btnReset() {
      this.$refs.productFormRef.resetFields()
      this.productCateValue = []
    },
    btnAdd() {
      this.$router.push('/product/productEdit')
    },
    btnView(row) {
      this.$router.push({
        path: '/product/productEdit',
        query: {
          id: row.id,
          handleType: 'view',
        },
      })
    },
    btnEdit(row) {
      this.$router.push({
        path: '/product/productEdit',
        query: {
          id: row.id,
          handleType: 'edit',
        },
      })
    },
    async btnDelete(row) {
      this.$confirm('此操作将删除选中的数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          //获取选中行id集合
          this.handleDelete(row.id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          })
        })
    },
    handleDelete(id) {
      let params = new URLSearchParams()
      params.append('id', id)
      api_deleteProduct(params).then((response) => {
        const res = response.data
        if (!res.success) {
          this.$message.error(res.msg)
          return
        }
        this.$message({
          type: 'success',
          message: res.msg,
        })
        this.btnSearch()
      })
    },
    handleCateChange() {},
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.btnSearch()
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val
      this.btnSearch()
    },
    //获取商品列表
    async handleProductList() {
      const param = {
        page: this.searchForm.pageNum,
        limit: this.searchForm.pageSize,
        bean: JSON.stringify(this.searchForm),
      }
      const { data: res } = await api_productList(param)
      this.productList = res.data.list
      this.page.total = res.data.total
    },
    //获取商品分类
    async handleProductCate() {
      const { data: res } = await api_listWithChildren()
      this.productCateOptions = res.data
    },
    //获取商品品牌
    async handleProductBrandList() {
      const { data: res } = await api_allBrandList()
      this.productBrandList = res.data
    },
    //修改商品标签
    async handleStatus(type, row) {
      let status
      if (type === 'onSale') {
        //上架
        // url = 'onSaleStatus'
        status = row.isOnSale
      } else if (type === 'new') {
        //新品
        // url = 'newStatus'
        status = row.isNew
      }
      let ids = []
      ids.push(row.id)
      const param = {
        ids: JSON.stringify(ids),
        type: type,
        status: status,
      }
      const { data: res } = await api_updateProductStatus(param)
      if (!res.success) {
        return this.$message.error(res.msg)
      }
      this.$message({
        message: res.msg,
        type: 'success',
      })
    },
  },
}
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}
.box-card {
  width: 100%;
  margin: auto;
}
.data-card {
  margin-top: 10px;
}

.el-date-picker {
  float: left;
}
</style>
